require("public/utils/wx-polyfill"), require("public/utils/page-enhance"), require("utils/wx-enhance");

var _user = _interopRequireDefault(require("public/utils/user")), _util = _interopRequireDefault(require("public/utils/util")), _socket = _interopRequireDefault(require("public/utils/socket")), _apiCode = _interopRequireDefault(require("public/utils/api-code")), _debounce = _interopRequireDefault(require("public/utils/debounce")), _throttle = _interopRequireDefault(require("public/utils/throttle")), _withinmsg = _interopRequireDefault(require("public/utils/withinmsg")), _reportAnalytics = require("public/utils/reportAnalytics"), _api = require("utils/api"), _config = _interopRequireDefault(require("utils/config")), _cardUtil = _interopRequireDefault(require("utils/card-util")), contactUtil = _interopRequireWildcard(require("utils/contact-util"));

function _interopRequireWildcard(e) {
    if (e && e.__esModule) return e;
    var t = {};
    if (null != e) for (var i in e) if (Object.prototype.hasOwnProperty.call(e, i)) {
        var a = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(e, i) : {};
        a.get || a.set ? Object.defineProperty(t, i, a) : t[i] = e[i];
    }
    return t.default = e, t;
}

function _interopRequireDefault(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

var systemInfo = wx.getSystemInfoSync(), isDevTools = "devtools" === systemInfo.platform, isIOS = -1 < systemInfo.system.indexOf("iOS");

Object.assign(global, {
    api: _api.api,
    json: _api.json,
    user: _user.default,
    util: _util.default,
    config: _config.default,
    apiCode: _apiCode.default,
    socket: _socket.default,
    debounce: _debounce.default,
    throttle: _throttle.default,
    regeneratorRuntime: require("./libs/regenerator/runtime-module")
});

var IOS_NAVBAR_HEIGHT = 44, ANDROID_NAVBAR_HEIGHT = 48, BAR_HEIGHT = systemInfo.statusBarHeight, NAVBAR_HEIGHT = isIOS ? IOS_NAVBAR_HEIGHT : ANDROID_NAVBAR_HEIGHT;

App({
    globalData: {
        BAR_HEIGHT: BAR_HEIGHT,
        NAVBAR_HEIGHT: NAVBAR_HEIGHT,
        NAVBAR_TOTAL_HEIGHT: BAR_HEIGHT + NAVBAR_HEIGHT,
        isDevTools: isDevTools,
        isIOS: isIOS,
        systemInfo: systemInfo,
        card: {},
        contact: {
            contact: {},
            contacttotal: 0,
            total: 0,
            site: {},
            isauthcontact: !1,
            isauthradar: !1
        },
        applyList: [],
        agent: {},
        get tokens() {
            return _user.default.getTokensSync();
        },
        model: _config.default.model,
        app: "site",
        WX_APP_NAME: "site",
        version: "2.12.12",
        appid: "wxb6eef443a6b04494",
        fromuid: "",
        environment: "wx",
        get fromapp() {
            return _config.default.WX_APP_NAME;
        },
        fromcardid: "",
        fromguid: "",
        fromsid: ""
    },
    onLaunch: function(e) {
        var i = this;
        console.log("当前版本: ", _config.default.appVersion), this.query = e ? _util.default.parseLaunchQueryString(e.query) : {}, 
        this.globalData.card = _cardUtil.default.getCardStorage(), _user.default.setDefaultParams({
            fromuid: this.query.fromuid,
            app: _config.default.WX_APP_NAME
        }), _withinmsg.default.initial({
            app: _config.default.WX_APP_NAME,
            appid: _config.default.WX_APP_ID
        }), contactUtil.loadContact(), _user.default.getTokens(function(e) {
            var t = function(e) {
                var t = e.accesstoken;
                return i.initSocket(t);
            };
            e ? t(e) : _user.default.listenToken(function(e) {
                t(e), i.addNewUserRecord();
            });
        }), this.checkVersionUpdate();
    },
    onPageNotFound: function() {
        wx.switchTab({
            url: "/pages/tabbar/card/card"
        });
    },
    onError: function(e) {},
    addNewUserRecord: function() {
        var e = this.query.fromuid;
        e && _api.api.minicard.spread.addnewuserrecord({
            data: {
                spreaduid: e,
                app: _config.default.WX_APP_NAME
            }
        });
    },
    addSpreadUser: function() {
        var e = this, t = this.query, i = t.agentid, a = t.from, r = void 0 === a ? 1 : a, o = t.agentuid, n = t.cardid, c = t.cardoplogid, u = this.globalData.card;
        !i && !n || u && u.cardid || (console.log({
            agentid: i,
            from: r,
            touid: o
        }, "新用户绑定服务商记录"), _api.api.minicard.agent.addspreaduser({
            data: {
                agentid: i,
                cardid: n,
                from: r,
                touid: o
            }
        }).then(function() {
            e.addSpreadLogs({
                cardid: n,
                type: 1 === r ? 8 : 9,
                relatedid: c
            });
        }));
    },
    addSpreadLogs: function(e, t) {
        if (t) {
            var i = this.query, a = i.cardoplogid, r = i.cardid, o = i.from;
            if (!a || !r) return;
            e = {
                relatedid: a,
                type: 1 === (void 0 === o ? 1 : o) ? 6 : 7,
                cardid: r
            };
        }
        console.log(e, "添加推广记录"), _api.api.minicard.card.addlogs({
            data: e
        });
    },
    onShow: function(e) {
        var t = this;
        this.handleScene(e), this.appNavigate(e.referrerInfo), this.globalData.shareTicket = e && e.shareTicket, 
        _user.default.getTokens().then(function() {
            var e = (0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : {}).accesstoken;
            return t.initSocket(e);
        });
    },
    appNavigate: function() {
        var e = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : {}, t = e.appId, i = e.extraData, a = void 0 === i ? {} : i;
        "wx0ac42dd945fd158d" === t ? this.handleMiniService(a) : this.miniapp(a);
    },
    handleMiniService: function(e) {
        var t = e.mode, i = e.guid, a = e.expireTime, r = this.globalData.card, o = r.version;
        if ("create" === t) {
            var n = Promise.resolve();
            this.reportAnalytics("radar_dredge_success"), i !== r.guid ? n = this.switchCorp(i) : contactUtil.loadContact(), 
            n.then(function() {
                wx.navigateTo({
                    url: "/pages/contacts/version/success/success?guid=".concat(i, "&version=").concat(o, "&expireTime=").concat(a)
                });
            });
        }
    },
    miniapp: function(e) {},
    handleScene: function(e) {
        switch (e.scene) {
          case 1038:
            _socket.default.reconnect();
            break;

          case 1035:
            this.reportAnalytics("official_into_card_view");
        }
    },
    onHide: function() {
        wx.interruptPush || this.updateViewSiteStatus();
    },
    updateViewSiteStatus: function() {
        var e = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : "exit", t = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : "";
        if (this.globalData.tokens.accesstoken) {
            var i = "view" === e ? {
                sid: t,
                op: e
            } : {
                op: e
            };
            _api.api.site.site.viewingsite({
                data: i
            }).then(function(e) {
                console.log(e);
            });
        }
    },
    initSocket: function(e) {
        e && _socket.default.connect(e);
    },
    collectFormId: function(e, t) {
        return isDevTools ? (console.info("devtool:collect-formid"), Promise.resolve()) : _api.api.weapp.collectformid({
            data: {
                formid: e.detail.formId,
                app: _config.default.WX_APP_NAME
            }
        });
    },
    recordCount: function(e, t) {
        e && _api.api.minicard.analysis.count({
            data: {
                cardid: e,
                type: t
            }
        });
    },
    count: {},
    openDebug: function(e) {
        var t = (this.count[e] || 0) + 1;
        console.log(t), 40 === t ? wx.setEnableDebug({
            enableDebug: !0
        }) : 21 === t && wx.setEnableDebug({
            enableDebug: !1
        }), this.count[e] = t;
    },
    loadContact: contactUtil.loadContact,
    switchCorp: contactUtil.switchCorp,
    getTokens: _user.default.getTokens,
    reportAnalytics: function(e) {
        var t = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : _reportAnalytics.ANALYTICS[e];
        if (console.info("report:event:".concat(e, ":").concat(JSON.stringify(t))), !isDevTools) return wx.reportAnalytics(e, t), 
        this.reportAnalyticsInMinicard(e, t);
    },
    reportAnalyticsInMinicard: function(e, t) {
        return _api.api.minicard.common.createeventtracking({
            data: {
                key: e,
                params: t
            }
        });
    },
    checkVersionUpdate: function() {
        if (wx.getUpdateManager) {
            var t = wx.getUpdateManager();
            t.onUpdateReady(function() {
                wx.showModal({
                    content: "小名片新版本准备好啦!",
                    confirmText: "立即体验",
                    cancelText: "我知道啦",
                    success: function(e) {
                        e.confirm && t.applyUpdate();
                    }
                });
            }), t.onUpdateFailed(function() {
                console.log("更新版本失败");
            });
        }
    }
});